package com.sduept.nwld.kpi.manager;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.kpi.model.KpiData;
@Service
public class KpiDataManager {
	@Autowired
	private SuperDAO dao;

	public <T> T createOrUpdate(T bean,Class<T> T){
		return dao.createOrUpdate(bean, T);
	}
	
	public <T> T create(T bean,Class<T> T){
		return dao.create(bean, T);
	}
	
	public <T> T update(T bean,Class<T> T){
		return dao.update(bean, T);
	}
	
	public <T> T findById(String id,Class<T> T){
		return dao.findById(id, T);
	}
	
	public <T> List<T> findByCondition(String where, String orderby,
			int[] range, Class<T> T){
		return dao.findByCondition(where, orderby, range, T);
	}
	
	public List<KpiData> findBydimension(List<String> volGrades,
			List<String> companys,List<String> times){
		StringBuilder quary = new StringBuilder();
		if(volGrades.size() > 0){
			quary.append("(");
			for(int i=0;i<volGrades.size();i++){
				quary.append(" voltageGrade = '");
				quary.append(volGrades.get(i));
				if(i == volGrades.size() - 1){
					quary.append("' ");
				}else{
					quary.append("' or");
				}
			}
			quary.append(") ");
		}
		if(volGrades.size() > 0 && companys.size() > 0){
			quary.append(" and ");
		}
		if(companys.size() > 0){
			quary.append("(");
			for(int i=0;i<companys.size();i++){
				quary.append(" company = '");
				quary.append(companys.get(i));
				if(i == companys.size() - 1){
					quary.append("' ");
				}else{
					quary.append("' or");
				}
			}
			quary.append(") ");
		}
		if(volGrades.size() > 0 && companys.size() > 0 && times.size() > 0){
			quary.append(" and ");
		}
		if(times.size() > 0){
			quary.append("(");
			for(int i=0;i<times.size();i++){
				quary.append(" time = '");
				quary.append(times.get(i));
				if(i == times.size() - 1){
					quary.append("' ");
				}else{
					quary.append("' or");
				}
			}
			quary.append(") ");
		}
		return dao.findByCondition(quary.toString(), KpiData.class);
	}
}
